package com.taptech.doufu.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.dialog.DoubleBtnTiptDialog;
import com.taptech.doufu.event.EventBusBackup;
import com.taptech.doufu.manager.NovelEditManager;
import com.taptech.doufu.bean.NovelEditModel;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.TimeDateFunction;

import org.greenrobot.eventbus.EventBus;

public class NovelEditHistoryDetailActivity extends DiaobaoBaseActivity {

    public static final String INTENT_KEY_NOVEL_DETAIL = "intent_key_novel_detail";
    private TextView tvContent,tvReset,tvCount,tvTime;

    private NovelEditModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_novel_edit_history_detail);
        tvContent = (TextView) findViewById(R.id.tvContent);
        tvReset = (TextView) findViewById(R.id.tvReset);
        tvCount = (TextView) findViewById(R.id.tvCount);
        tvTime = (TextView) findViewById(R.id.tvTime);
        model = (NovelEditModel) getIntent().getSerializableExtra(INTENT_KEY_NOVEL_DETAIL);
        if (model != null) {
            if (!TextUtils.isEmpty(model.getContent())) {
                int count = DiaobaoUtil.getStringSizeNoNull(model.getContent());
                tvContent.setText(model.getContent());
                tvCount.setText(count+"字");
            }

            if (model.getCreated_at() > 0) {
                tvTime.setText(TimeDateFunction.toDateTime(model.getCreated_at()*1000));
            }

            if (model.getFlag() == NovelEditManager.NOVEL_SECTION) {
                tvReset.setVisibility(View.GONE);
            }else{
                tvReset.setVisibility(View.VISIBLE);
            }
        }else{
            tvReset.setVisibility(View.GONE);
        }

    }


    public void onReset(View view) {
        if (model == null) {
            Toast.makeText(this,"历史数据异常",Toast.LENGTH_LONG).show();
            return;
        }
        DoubleBtnTiptDialog doubleBtnLongTextDialog = new DoubleBtnTiptDialog(this,
                R.style.updateDialog, "当前的操作将还原此条历史，还原此条历史成功后，将与“正式存稿”替换，“正式存稿”将生成一条新历史。", "确定", new DoubleBtnTiptDialog.CallBack() {
            @Override
            public void onBack() {
                EventBusBackup eventBusBackup = new EventBusBackup();
                eventBusBackup.setModel(model);
                eventBusBackup.setTag(EventBusBackup.tag);
                EventBus.getDefault().post(eventBusBackup);
                finish();
            }
        });
        doubleBtnLongTextDialog.show();

    }

}
